import React, { useState, forwardRef, useImperativeHandle } from 'react'
import { Form, Radio } from 'antd'
import { TextWidget } from '@inbiz/react';
import FormItemTitle from '../components/FormItemTitle'
import './index.less'
export default forwardRef(function PermType(props, ref) {
  const { config, $params, categoryLevel, $categoryData, selected } = props
  const [form] = Form.useForm()
  const [clearPermType, $clearPermType] = useState(false)
  const [needNewPanel, $needNewPanel] = useState(false)
  const clearField = () => {
    if (needNewPanel) {
      categoryLevel.current = -1
    }
    
    $needNewPanel(false)
    $clearPermType(false)
    form.resetFields()
    $params(p => ({ ...p, PageIndex: 1, PermType: 0 }))
  }
  const onValuesChange = (e) => {
    if (selected.length > 0) {
      $needNewPanel(false)
    } else {
      $needNewPanel(true)
      
      
    }
    $clearPermType(e.PermType)
    $params(p => ({ ...p, PageIndex: 1, PermType: e.PermType }))
  }
  useImperativeHandle(ref, () => ({
    resetAll: clearField,
    permTypeForm: form
  }))

  return (
    <div className='permtype'>
      <Form onValuesChange={onValuesChange} form={form}>
        <>
          <FormItemTitle
            title={'SearchResultEngine.authorityFilter'}
            clearField={() => clearField()}
            showClear={clearPermType}
            config={config}
          />
          <Form.Item name='PermType'>
            <Radio.Group>
              <Radio value={2}><TextWidget>SearchResultEngine.hasPermission</TextWidget></Radio><br />
              <Radio value={1}><TextWidget>SearchResultEngine.noPermission</TextWidget></Radio>
            </Radio.Group>
          </Form.Item>
        </>
      </Form>
    </div>
  )
})
